﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using BO = Viewer.BusinessObject.BusinessEntities;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class QUNhomQuyenByToChuc_ToChuc : BusinessBase
    {
        //private string _tenToChucShort;
        //public string TenToChucShort
        //{
        //    set { _tenToChucShort = value; }
        //    get {
        //        _tenToChucShort = this.NSToChuc.TenToChucShort; 
        //        return _tenToChucShort; 
        //    }
        //}

        public bool IsQuyenView { get; set; }
        public bool IsQuyenEdit { get; set; }
        public bool IsQuyenAdd { get; set; }
        public bool IsQuyenDelete { get; set; }

        public void Update()
        {

        }

        public List<QUNhomQuyenByToChuc_ToChuc> GetAll()
        {
            return this.Context.QUNhomQuyenByToChuc_ToChucs.ToList();
        }

        public List<QUNhomQuyenByToChuc_ToChuc> GetByNhomQuyenByToChucId(int pNhomQuyenByToChucId)
        {
            return this.Context.QUNhomQuyenByToChuc_ToChucs.Where(c => c.NhomQuyenByToChucID == pNhomQuyenByToChucId).ToList();
        }

        public QUNhomQuyenByToChuc_ToChuc GetQuyenByToChucIDandUserID(int pToChucID,int pUserID)
        {
            var tmpListTongHopQuyen = (from nqtc_tc in this.Context.QUNhomQuyenByToChuc_ToChucs
                                       join nqtc in this.Context.QUNhomQuyenByToChucs on nqtc_tc.NhomQuyenByToChucID equals nqtc.NhomQuyenByToChucID
                                       join nqtc_nd in this.Context.QUNhomQuyenByToChuc_NguoiDungs on nqtc.NhomQuyenByToChucID equals nqtc_nd.NhomQuyenByToChucID
                                       where nqtc_nd.NguoiDungID == pUserID
                                       && nqtc_tc.ToChucID == pToChucID
                                       select nqtc_tc).ToList();

            QUNhomQuyenByToChuc_ToChuc nhomQuyenTheoToChuc = new QUNhomQuyenByToChuc_ToChuc();
            nhomQuyenTheoToChuc.IsQuyenAdd = false;
            nhomQuyenTheoToChuc.IsQuyenDelete = false;
            nhomQuyenTheoToChuc.IsQuyenEdit = false;
            nhomQuyenTheoToChuc.IsQuyenView = false;

            foreach (var item in tmpListTongHopQuyen)
            {
                nhomQuyenTheoToChuc.IsQuyenAdd = nhomQuyenTheoToChuc.IsQuyenAdd || item.IsAdd;
                nhomQuyenTheoToChuc.IsQuyenView = nhomQuyenTheoToChuc.IsQuyenView || item.IsView;
                nhomQuyenTheoToChuc.IsQuyenEdit = nhomQuyenTheoToChuc.IsQuyenEdit || item.IsEdit;
                nhomQuyenTheoToChuc.IsQuyenDelete = nhomQuyenTheoToChuc.IsQuyenDelete || item.IsDelete;
            }

            if (pUserID == 0)
            {
                nhomQuyenTheoToChuc.IsQuyenAdd = true;
                nhomQuyenTheoToChuc.IsQuyenDelete = true;
                nhomQuyenTheoToChuc.IsQuyenEdit = true;
                nhomQuyenTheoToChuc.IsQuyenView = true;
            }

            return nhomQuyenTheoToChuc;
        }

        public void SaveData(int pNhomQuyenByToChucId, List<QUNhomQuyenByToChuc_ToChuc> lstNewItems)
        {
            
//            var lstOldData = this.Context.QUNhomQuyenByToChuc_ToChucs.Where(c => c.NhomQuyenByToChucID == pNhomQuyenByToChucId).ToList();
            var lstOldData = GetByNhomQuyenByToChucId(pNhomQuyenByToChucId);
            foreach (var oOldItem in lstOldData)
            {
                this.Context.QUNhomQuyenByToChuc_ToChucs.DeleteOnSubmit(oOldItem);    
            }
            
            foreach (var oNewItem in lstNewItems)
            {
                this.Context.QUNhomQuyenByToChuc_ToChucs.InsertOnSubmit(oNewItem);
            }
             
            this.Context.SubmitChanges();
            
        }

    }
}